﻿using System;
using Enums;
using XF.ExControls;
using XF.Common;
using System.Windows.Forms;

namespace KYBM
{
    public partial class FrmDialogPriceList : XFFormEx
    {
        private readonly XF.BLL.Bill_DTOItems bll = new XF.BLL.Bill_DTOItems();
        private int _customerId;
        private int _materielId;
        public decimal Price { get; set; } = 0;

        public FrmDialogPriceList(int customerId,int materielId)
        {
            InitializeComponent();
            this._customerId = customerId;
            this._materielId = materielId;
        }

        private void tsBtnSure_Click(object sender, EventArgs e)
        {
            if (zDataConverter.ToDecimal(tbPrice.Text.Trim(), -999) == -999)
            {
                QQMessageBox.Show(
                        this,
                        MessageText.CHECK_INPUT_DTO_PRICE,
                        MessageText.MESSAGEBOX_CAPTION_TIP,
                        QQMessageBoxIcon.Information,
                        QQMessageBoxButtons.OK);
                return;
            }
            Price = zDataConverter.ToDecimal(tbPrice.Text.Trim());
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void FrmDialogPriceList_Load(object sender, EventArgs e)
        {
            xfDataGridView1.AutoGenerateColumns = false;
            tbPrice.Text = Price.ToString();
            BindData();
        }

        private void BindData()
        {
            xfDataGridView1.DataSource = bll.GetDetailList(" CustomerId = " + _customerId + " and MaterielId = " + _materielId).Tables[0];
            foreach (DataGridViewRow dgvr in xfDataGridView1.SelectedRows)
            {
                dgvr.Selected = false;
            }
        }

        private void tsBtnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void xfDataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                tbPrice.Text = zDataConverter.ToString(xfDataGridView1.Rows[e.RowIndex].Cells[ColPrice.Name].Value);
            }
        }

        private void FrmDialogPriceList_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    tsBtnSure_Click(this, EventArgs.Empty);
                    break;
                case Keys.Escape:
                    tsBtnCancel_Click(this, EventArgs.Empty);
                    break;
            }
        }
    }
}
